package io.adrainty.bolee.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.adrainty.bolee.file.feign.IFileBusinessFeign;
import io.adrainty.bolee.file.models.entity.FileEntity;
import io.adrainty.bolee.framework.commons.basic.ResultDTO;
import io.adrainty.bolee.framework.commons.utils.ResultDTOUtil;
import io.adrainty.bolee.insurance.models.entity.BannerEntity;
import io.adrainty.bolee.insurance.repository.IBannerRepository;
import io.adrainty.bolee.insurance.service.IBannerService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>BannerServiceImpl</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description IBannerService
 * @since 2025/8/18 16:08:08
 */

@Service
public class BannerServiceImpl implements IBannerService {

    @Resource
    private IFileBusinessFeign fileBusinessFeign;

    @Resource
    private IBannerRepository bannerRepository;

    @Override
    public IPage<BannerEntity> pageBanner(Integer pageNum, Integer pageSize, BannerEntity param) {
        IPage<BannerEntity> bannerList = bannerRepository.pageBanner(pageNum, pageSize, param);
        fillBannerData(bannerList.getRecords());
        return bannerList;
    }

    @Override
    @Transactional
    public BannerEntity saveBanner(BannerEntity bannerVO) {
        Long id = bannerRepository.saveBanner(bannerVO);
        bannerVO.setId(id);
        List<FileEntity> fileEntities = bannerVO.getFileVOs();
        fileEntities.forEach(fileEntity -> fileEntity.setBusinessId(id));
        ResultDTO<List<FileEntity>> resultDTO = fileBusinessFeign.bindBatchFile(fileEntities);
        fileEntities = ResultDTOUtil.assertResultDataNotNull(resultDTO);
        bannerVO.setFileVOs(fileEntities);
        return bannerVO;
    }

    @Override
    public Boolean updateBanner(BannerEntity bannerVO) {
        bannerRepository.updateBannerById(bannerVO);
        ResultDTO<Boolean> resultDTO = fileBusinessFeign.replaceBindBatchFile(bannerVO.getFileVOs());
        return ResultDTOUtil.assertResultDataNotNull(resultDTO);
    }

    @Override
    @Transactional
    public Boolean deleteBanner(List<Long> bannerIds) {
        if (CollUtil.isEmpty(bannerIds)) return Boolean.TRUE;
        bannerRepository.deleteBatchBanner(bannerIds);
        ResultDTO<Boolean> resultDTO = fileBusinessFeign.deleteInBusinessIds(bannerIds);
        return ResultDTOUtil.assertResultDataNotNull(resultDTO);
    }

    /**
     * 填充数据
     *
     * @param records 数据列表
     */
    private void fillBannerData(List<BannerEntity> records) {
        if (CollUtil.isEmpty(records)) return;
        List<Long> bannerIds = records.stream().map(BannerEntity::getId).toList();
        ResultDTO<List<FileEntity>> resultDTO = fileBusinessFeign.findInBusinessIds(bannerIds);
        Map<Long, List<FileEntity>> collect = ResultDTOUtil.assertResultDataNotNull(resultDTO).stream()
                .collect(Collectors.groupingBy(FileEntity::getBusinessId));
        for (BannerEntity banner : records) {
            banner.setFileVOs(collect.getOrDefault(banner.getId(), new ArrayList<>()));
        }
    }

}
